﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Comissoes.Models;

namespace Comissoes.Security
{
    public abstract class Access
    {
        /// <summary>
        /// Informa se há usuário logado
        /// </summary>
        public static bool IsLogged
        {
            get
            {
                return HttpContext.Current.Session["UserLogin"] != null;
            }
        }

        /// <summary>
        /// Retorna os dados do usuário logado
        /// </summary>
        public static OPERADORES User
        {
            get
            {
                xcontEntities db = new xcontEntities();

                if (IsLogged)
                {
                    Int32 ID_OPER = Int32.Parse(HttpContext.Current.Session["UserLogin"].ToString());

                    return db.OPERADORES.Include("PESSOAS").Include("PERFIS").FirstOrDefault(t => t.ID_OPER.Equals(ID_OPER));
                }
                else
                    return null;
            }
        }

        /// <summary>
        /// Registra o login do operador
        /// </summary>
        /// <returns>Dados do operador</returns>
        public static void Login(OPERADORES user)
        {
            HttpContext.Current.Session.Add("UserLogin", user.ID_OPER);
        }

        /// <summary>
        /// Registra o logout do operador
        /// </summary>
        public static void Logout()
        {
            HttpContext.Current.Session.Abandon();
        }
    }
}
